/**
 * PageMarket
 * TrandActivity.java
 * com.android.pagemarket.ui.activity.trend
 * 
 */
package com.android.pagemarket.ui.activity.trend;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.pagemarket.R;
import com.android.pagemarket.business.model.Message;
import com.android.pagemarket.ui.activity.base.LoadableActivity;
import com.android.pagemarket.ui.activity.main.MainIndexActivity;
import com.android.pagemarket.ui.adapter.TrandListAdapter;
import com.android.pagemarket.ui.adapter.TrandListAdapter.OnHeadMsgListenr;
import com.android.pagemarket.ui.widget.TitleBar;
import com.android.pagemarket.util.ImageLoader;
import com.android.pagemarket.util.ImageLoader.Imager;

/*********************************************************
 * @author : zhaohua
 * @version : 2012-10-29
 * @see : 同舟动态视图
 * @Copyright : copyrights reserved by personal 2007-2012
 **********************************************************/
public class TrandActivity extends LoadableActivity implements OnClickListener, OnHeadMsgListenr
{
    private Button btn_promot;
    private Button btn_trand;
    private View   head_lay;
    private ListView mListView;
    private TrandListAdapter mListAdapter;
    private boolean isTrand = false;
    private boolean isCreated = false;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_trend);
        initViews();
        mLoadingView.showLoadingView();
        load(true);
        load(false);
    }
    
    @Override
    protected void onResume()
    {
        super.onResume();
        if(!isCreated)
        {
            isCreated = true;
        }else
        {
//            mListAdapter.loadDataDB(isTrand);
            load(isTrand);
        }
    }
    
    @Override
    public void onBackPressed()
    {
        if( getParent() instanceof MainIndexActivity)
        {
            ((MainIndexActivity)getParent()).onBackPressed();
        }
    }
    
    @Override
    public void refreshView()
    {
        load(isTrand);
    }
    
    @Override
    public void onClick(View v)
    {
        switch(v.getId())
        {
            case R.id.btn_trand:
            {
                if(!isTrand)
                {
                    btn_trand.setSelected(true);
                    btn_promot.setSelected(false);
                    isTrand = true;
                    
                    load(true);
                }
                break;
            }
            case R.id.btn_promot:
            {
                if(isTrand)
                {
                    btn_trand.setSelected(false);
                    btn_promot.setSelected(true);                   
                    isTrand = false;
                    
                    load(false);
                }
                break;
            }
        }
    }
    
    @Override
    public void onHeaderMsg(Message msg)
    {
        mLoadingView.showMainView();
        if(msg != null)
        {
            showHeadMsg(msg);
        }else
        {
            // 没有头条
            head_lay.setVisibility(View.GONE);
            /**
            ContentManager.getInstance().getHeadMessage(new NotifyListener<Message>()
            {
                @Override
                public void onSucceed(Message result)
                {
                    if(result == null)
                    {
                        head_lay.setVisibility(View.GONE);
                    }else
                    {
                        showHeadMsg(result);
                    }
                }
                @Override
                public void onError(int resultCode)
                {
                    
                }
            });*/
        }
    }  
    
    @Override
    public void onShow()
    {
        mLoadingView.showMainView();
    }
    
    @Override
    protected void setupTitleBar(TitleBar titleBar)
    {
        titleBar.setTitle(R.string.tab_trand);
        titleBar.setLeftVisible(false);
        titleBar.setRightVisible(false);
    }
    
    private void showHeadMsg(Message msg)
    {
        head_lay.setVisibility(View.VISIBLE);
        
        ImageView image = getViewById(R.id.image);
        TextView desc = getViewById(R.id.desc);
        if(msg.imageURL != null)
        {
            ImageLoader.getInstance().DisplayImage(new Imager(msg.imageURL, image, 150, R.drawable.ic_pic_default));
        }else
        {
            image.setVisibility(View.GONE);
        }
        desc.setText(msg.content);        
    }
    
    private void initViews()
    {
        head_lay   = getViewById(R.id.head_lay);
        btn_promot = getViewById(R.id.btn_promot);
        btn_trand = getViewById(R.id.btn_trand);
        mListView = getViewById(R.id.listView);
        mListAdapter = new TrandListAdapter(this, mListView);
        mListView.setAdapter(mListAdapter);
        mListAdapter.setHeadListener(this);
        
        head_lay.setVisibility(View.GONE);
        btn_promot.setSelected(true);
        btn_trand.setSelected(false);
    }
    
    private void load(boolean isTrand)
    {
        mListAdapter.loadData(isTrand);
    }
}
